//
//  YWSearchVoiceViewController.m
//  聆听FM
//
//  Created by dllo on 16/6/21.
//  Copyright © 2016年 Dark. All rights reserved.
//

#import "YWSearchVoiceViewController.h"
#import "YWAnchorVoiceTableViewCell.h"
#import "YWSearchModel.h"
#import "ZCSListenListListModel.h"
#import "ZCSPlayMusicViewController.h"

@interface YWSearchVoiceViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, copy) NSString *condition;
@property (nonatomic, assign) NSInteger page;
@property (nonatomic, strong) UISegmentedControl *seg;
@property (nonatomic, strong) UITableView *albumTableView;
@property (nonatomic, assign) NSInteger maxPage;
@property (nonatomic, strong) NSMutableArray *albumArray;

@property (nonatomic, strong) NSMutableArray *playArray;

@end

@implementation YWSearchVoiceViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self getView];
    [self getData];
}

- (void)getView
{
    [self getSegmentControl];
    [self getAlbumTableView];
}

- (void)getSegmentControl
{
    self.seg = [[UISegmentedControl alloc] initWithItems:@[@"相关度", @"最多播放", @"最新上传"]];
    _seg.frame = CGRectMake(0, 0, SCREENWIDTH, 30 * FITHEIGHT);
    _seg.selectedSegmentIndex = 0;
    [_seg addTarget:self action:@selector(didClickSegmentControl:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:_seg];
}

- (void)didClickSegmentControl:(UISegmentedControl *)seg
{
    switch (seg.selectedSegmentIndex) {
        case 0:
            [_albumArray removeAllObjects];
            [_albumTableView reloadData];
            [_albumTableView setContentOffset:CGPointMake(0, 0)];
            _condition = @"relation";
            _page = 1;
            [self getDataByCondition:_condition Page:_page];
            break;
        case 1:
            [_albumArray removeAllObjects];
            [_albumTableView reloadData];
            [_albumTableView setContentOffset:CGPointMake(0, 0)];
            _condition = @"play";
            _page = 1;
            [self getDataByCondition:_condition Page:_page];
            break;
        case 2:
            [_albumArray removeAllObjects];
            [_albumTableView reloadData];
            [_albumTableView setContentOffset:CGPointMake(0, 0)];
            _condition = @"recent";
            _page = 1;
            [self getDataByCondition:_condition Page:_page];
            break;
        default:
            break;
    }
}

- (void)getAlbumTableView
{
    self.albumTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 30 * FITHEIGHT, SCREENWIDTH, SCREENHEIGHT - 64 - 35 - 30 * FITHEIGHT) style:UITableViewStylePlain];
    _albumTableView.delegate = self;
    _albumTableView.dataSource = self;
    _albumTableView.separatorStyle = NO;
    [self.view addSubview:_albumTableView];
    
    [_albumTableView registerClass:[YWAnchorVoiceTableViewCell class] forCellReuseIdentifier:@"voiceCell"];
    
    _albumTableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        _page++;
        if (_page > _maxPage) {
            [_albumTableView.mj_footer endRefreshingWithNoMoreData];
        }
        else
        {
            [self getDataByCondition:_condition Page:_page];
        }
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _albumArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    YWSearchModel *searchModel = [_albumArray objectAtIndex:indexPath.row];
    NSLog(@"%ld", [searchModel.title length]);
    if ([searchModel.title length] > 17) {
        return 100 * FITHEIGHT;
    }
    else
    {
        return 80 * FITHEIGHT;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    YWAnchorVoiceTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"voiceCell"];
    cell.searchModel = [_albumArray objectAtIndex:indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    ZCSPlayMusicViewController *play = [ZCSPlayMusicViewController sharePlayVC];
    play.musicIndex = indexPath.row;
    play.dataArray = self.playArray;
    play.albumId = [[self.albumArray objectAtIndex:indexPath.row] album_id];
    [self presentViewController:play animated:YES completion:nil];
}

- (void)getData
{
    _condition = @"relation";
    _page = 1;
    self.playArray = [NSMutableArray array];
    self.albumArray = [NSMutableArray array];
    [self getDataByCondition:_condition Page:_page];
}

- (void)getDataByCondition:(NSString *)condition Page:(NSInteger)page
{
    NSString *urlString = [NSString stringWithFormat:@"http://search.ximalaya.com/front/v1?condition=%@&core=track&device=iPhone&kw=%@&live=true&page=%ld&paidFilter=false&rows=20", condition, _keyString, page];
    NSLog(@"%@", urlString);
    [LTNetTool GetNetWithURL:urlString Body:nil Header:nil Response:LTJSON Success:^(id result) {
        NSDictionary *responseDic = [result objectForKey:@"response"];
        NSInteger numFound = [[responseDic objectForKey:@"numFound"] integerValue];
        if (numFound != 0) {
            NSArray *tempArray = [responseDic objectForKey:@"docs"];
            _maxPage = numFound / 20;
            //余数
            NSInteger remainder = numFound % tempArray.count;
            if (remainder != 0) {
                _maxPage++;
            }
            for (NSDictionary *dic in tempArray) {
                YWSearchModel *searchModel = [[YWSearchModel alloc] initWithDic:dic];
                ZCSListenListListModel *model = [[ZCSListenListListModel alloc] initWithDic:dic];
                [_playArray addObject:model];
                [_albumArray addObject:searchModel];
            }
            [_albumTableView reloadData];
            [_albumTableView.mj_footer endRefreshing];
        }
    } Failure:^(NSError *error) {
        NSLog(@"请求失败");
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
